package com.sftz.modules.minapp.personNotice.service;

import java.util.List;
import java.util.Map;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.google.common.collect.Maps;
import com.sftz.assets.entities.CourseEntity;
import com.sftz.assets.entities.InstitutionEntity;
import com.sftz.assets.entities.inform.NoticeContentEntity;
import com.sftz.assets.entities.inform.NoticeUserEntity;
import com.sftz.assets.enums.CourseStateEnum;
import com.sftz.assets.enums.NoticeTypeEnum;
import com.sftz.assets.enums.ReadStatusEnum;
import com.sftz.modules.minapp.personNotice.dto.PersonNoticeDto;
import com.sftz.modules.minapp.personNotice.form.PersonNoticeForm;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.util.DbSqlUtil;
import com.uxuexi.core.web.base.service.BaseService;

@IocBean
public class PersonNoticeViewService extends BaseService<NoticeContentEntity> {
	private static final Log log = Logs.get();

	//个人设置公告列表
	public Map<String, Object> findPersonNoticeList(PersonNoticeForm form, Integer userId) {

		Map<String, Object> obj = Maps.newHashMap();
		//计算起始页
		int start = (form.getPageNum() - 1) * form.getPageSize();
		//查询全部公告列表
		Sql sql = Sqls.create(sqlManager.get("minapp_personNotice_list"));
		sql.setParam("delete", ReadStatusEnum.DELETE.intKey());
		sql.setParam("systemNotice", NoticeTypeEnum.SYSTEM.intKey());
		sql.setParam("insNotice", NoticeTypeEnum.INSTITUTION.intKey());
		sql.setParam("courseNotice", NoticeTypeEnum.COURSE.intKey());
		sql.setParam("publishStatus", CourseStateEnum.PUBLISHED.intKey());
		sql.setParam("userId", userId);
		//分页查询
		sql.setParam("start", start);
		sql.setParam("end", form.getPageSize());
		List<PersonNoticeDto> noticeList = DbSqlUtil.query(dbDao, PersonNoticeDto.class, sql);
		//已读未读标识
		if (!Util.isEmpty(noticeList)) {
			for (PersonNoticeDto p : noticeList) {
				NoticeUserEntity pu = dbDao.fetch(NoticeUserEntity.class,
						Cnd.where("noticeId", "=", p.getId()).and("userId", "=", userId));
				if (!Util.isEmpty(pu)) {
					if (pu.getStatus().equals(ReadStatusEnum.READ.intKey())) {
						p.setReadStatus(ReadStatusEnum.READ.intKey());
					} else {
						p.setReadStatus(ReadStatusEnum.UNREAD.intKey());
					}
				}
			}
		}

		obj.put("noticeList", noticeList);

		//查询总数
		Sql countSql = Sqls.create(sqlManager.get("minapp_personNotice_list_count"));
		countSql.setParam("delete", ReadStatusEnum.DELETE.intKey());
		countSql.setParam("systemNotice", NoticeTypeEnum.SYSTEM.intKey());
		countSql.setParam("insNotice", NoticeTypeEnum.INSTITUTION.intKey());
		countSql.setParam("courseNotice", NoticeTypeEnum.COURSE.intKey());
		countSql.setParam("publishStatus", CourseStateEnum.PUBLISHED.intKey());
		countSql.setParam("userId", userId);
		int pageCount = DbSqlUtil.fetchInt(dbDao, countSql);

		obj.put("pageCountone", pageCount);
		obj.put("pageNum", form.getPageNum());
		obj.put("pageSize", form.getPageSize());

		return obj;

	}

	//删除公告
	public Object delUserNoticeById(PersonNoticeForm form, Integer noticeId, Integer userId) {
		//下一页
		int pageNum = form.getPageNum() + 1;
		form.setPageNum(pageNum);
		//返回公告列表
		Map<String, Object> findPersonNoticeList = findPersonNoticeList(form, userId);
		List<PersonNoticeDto> noticeList = (List<PersonNoticeDto>) findPersonNoticeList.get("noticeList");
		PersonNoticeDto personNoticeDto = null;
		//返回下一页第一条数据
		if (!Util.isEmpty(noticeList)) {
			personNoticeDto = noticeList.get(0);
		}

		//已读和删除的公告数据会在用户公告表中
		//1查询用户公告表中是否有此数据
		NoticeUserEntity fetch = dbDao.fetch(NoticeUserEntity.class,
				Cnd.where("noticeId", "=", noticeId).and("userId", "=", userId));
		if (!Util.isEmpty(fetch)) {
			//2有，修改状态
			dbDao.update(NoticeUserEntity.class, Chain.make("status", ReadStatusEnum.DELETE.intKey()),
					Cnd.where("id", "=", fetch.getId()));
		} else {
			//2没有，添加数据
			NoticeUserEntity n = new NoticeUserEntity();
			n.setNoticeId(noticeId);
			n.setUserId(userId);
			n.setStatus(ReadStatusEnum.DELETE.intKey());
			dbDao.insert(n);
		}

		return personNoticeDto;

	}

	//单击链接标记已读
	public Object fetchInfoAndUpdateRead(Integer noticeId, Integer userId) {
		//已读和删除的公告数据会在用户公告表中
		//1查询用户公告表中是否有此数据
		NoticeUserEntity fetch = dbDao.fetch(NoticeUserEntity.class,
				Cnd.where("noticeId", "=", noticeId).and("userId", "=", userId));
		if (!Util.isEmpty(fetch)) {
			//2有，修改状态
			dbDao.update(NoticeUserEntity.class, Chain.make("status", ReadStatusEnum.READ.intKey()),
					Cnd.where("id", "=", fetch.getId()));
		} else {
			//2没有，添加数据
			NoticeUserEntity nu = new NoticeUserEntity();
			nu.setNoticeId(noticeId);
			nu.setUserId(userId);
			nu.setStatus(ReadStatusEnum.READ.intKey());
			dbDao.insert(nu);
		}

		return fetchInfo(noticeId);

	}

	//公告详情
	public Object fetchInfo(int noticeId) {
		Map<String, Object> obj = Maps.newHashMap();
		NoticeContentEntity notice = dbDao.fetch(NoticeContentEntity.class, Cnd.where("id", "=", noticeId));
		if (!Util.isEmpty(notice)) {
			Integer noticeType = notice.getNoticeType();
			//课程信息
			if (noticeType.equals(NoticeTypeEnum.COURSE.intKey())) {
				CourseEntity course = dbDao.fetch(CourseEntity.class, Cnd.where("id", "=", notice.getCourseId()));
				obj.put("course", course);
			} else if (noticeType.equals(NoticeTypeEnum.INSTITUTION.intKey())) {
				InstitutionEntity instituion = dbDao.fetch(InstitutionEntity.class,
						Cnd.where("id", "=", notice.getInstitutionId()));
				obj.put("instituion", instituion);
			}
		}
		obj.put("notice", notice);
		return obj;
	}
}